﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;

namespace DomainComponents.Common {
    //[DomainComponent]
    //public interface IPhone {
    //    string PhoneType { get; set; }
    //    string Number { get; set; }
    //}

    [DomainComponent]
    [NonPersistentDc]
    public interface IPhones {
        string OtherPhone { get; set; }
        string MobilePhone { get; set; }
        string OfficePhone { get; set; }
        string HomePhone { get; set; }
        string Fax { get; set; }

        //[DevExpress.ExpressApp.DC.Aggregated]
        //[VisibleInDetailView(false)]
        //IList<IPhone> Phones { get; }

        void CopyTo(IPhones targetPhones);
    }

    [DomainLogic(typeof(IPhones))]
    public class PhonesLogic {
        //public static Type PhoneObjectType = typeof(IPhone);

        //public const string OtherPhoneId = "Other";
        //public const string MobilePhoneId = "Mobile";
        //public const string OfficePhoneId = "Office";
        //public const string HomePhoneId = "Home";
        //public const string FaxId = "Fax";

        //public static void AfterConstruction(IPhones phones, IObjectSpace os) {
        //    IPhone phone1 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone1.PhoneType = OtherPhoneId;
        //    phones.Phones.Add(phone1);
        //    IPhone phone2 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone2.PhoneType = MobilePhoneId;
        //    phones.Phones.Add(phone2);
        //    IPhone phone3 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone3.PhoneType = OfficePhoneId;
        //    phones.Phones.Add(phone3);
        //    IPhone phone4 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone4.PhoneType = HomePhoneId;
        //    phones.Phones.Add(phone4);
        //    IPhone phone5 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone5.PhoneType = FaxId;
        //    phones.Phones.Add(phone5);
        //}

        public static void CopyTo(IPhones self, IObjectSpace objectSpace, IPhones targetPhones) {
            if(targetPhones == null) {
                return;
            }
            targetPhones.OtherPhone = self.OtherPhone;
            targetPhones.MobilePhone = self.MobilePhone;
            targetPhones.OfficePhone = self.OfficePhone;
            targetPhones.HomePhone = self.HomePhone;
            targetPhones.Fax = self.Fax;

            //foreach(IPhone phone in self.Phones) {
            //    if(targetPhones.Phones[phone.PhoneType] != null) {
            //        targetPhones.Phones[phone.PhoneType].Number = phone.Number;
            //    }
            //    else {
            //        IPhone targetPhone = (IPhone)objectSpace.CreateObject(PhonesLogic.PhoneObjectType);
            //        targetPhone.PhoneType = phone.PhoneType;
            //        targetPhones.Phones.Add(targetPhone);
            //    }
            //}
        }
        //public static string GetPhoneByType(IList<IPhone> phones, string phoneType) {
        //    foreach(IPhone phone in phones) {
        //        if(phone.PhoneType == phoneType)
        //            return phone.Number;
        //    }
        //    return null;
        //}

        //public static string Get_OtherPhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, OtherPhoneId);
        //}
        //public static string Get_MobilePhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, MobilePhoneId);
        //}
        //public static string Get_OfficePhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, OfficePhoneId);
        //}
        //public static string Get_HomePhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, HomePhoneId);
        //}
        //public static string Get_Fax(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, FaxId);
        //}
    }
}
